/*
 * Copyright 2014 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package Server.Agent;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import Server.Model.Hall.dthall;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import redis.clients.jedis.Jedis;

public final class WebSocketServer {

	static final boolean SSL = System.getProperty("ssl") != null;
	static final int PORT = Integer.parseInt(System.getProperty("port", SSL ? "8443" : "9300"));
	public static Thread _thread;

	public static void Run() {
		_thread = new Thread(new Runnable() {
			@Override
			public void run() {
				while (true) { 
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					try {
						dthall.getInstance().OnDeal(1000);

					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		});
		_thread.start();
	}

    public static void main(String[] args) throws Exception {
    	//Server.Model.ZJMJ.JHMJ.MajHelper.Test();
    	//Server.Model.GDMJ.TuiDaoHu.MajHelper.Test();
    	//Server.Model.Poker.ZJH.GameLogic.Test();
    	//if(true)    	return;
    	
    	Run();
        // Configure SSL.
        final SslContext sslCtx;
        if (SSL) {
            SelfSignedCertificate ssc = new SelfSignedCertificate();
            sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
        } else {
            sslCtx = null;
        }
        
        /*
        try
        {
    		Jedis jedis = new Jedis("localhost",6379);
    		jedis.auth("Zying123");
    		if(jedis.isConnected())
    		{    			
    			jedis.set("foo", "bar");
    			String value = jedis.get("foo");	
    			System.out.print(value);
    		}	
        }catch(Exception e)
        {
        	e.printStackTrace();
        	System.out.print(e.getMessage());
        }
        */
        dthall.getInstance().readRobotName();   
    
	    int tcp_port = PORT;
	    String sPort = GameConfig.getInstance().get("TCP_PORT");
	    if(sPort.length() > 0)
	    {
	    	tcp_port = Integer.parseInt(sPort);
	    }
        
        Log log = LogFactory.getLog("rootLogger"); 
        log.error("Init main");
		
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
             .channel(NioServerSocketChannel.class)
             .childHandler(new WebSocketServerInitializer(sslCtx));

            Channel ch = b.bind(tcp_port).sync().channel();

            System.out.println("Open your web browser and navigate to " +
                    (SSL? "https" : "http") + "://127.0.0.1:" + sPort + '/');

            ch.closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
